﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace CM_Classes
{
    public class Attrib
    {
        string connectionString = @"Data Source=t-sql;Initial Catalog=ConfigurationManager;Integrated Security=True";

        // properties
        private int attribID;
        private string attribName;
        private string dataType;
        private bool attribDisabled; 
        private string attribDescription;

        

        public int AttribID
        {
            get { return attribID; }
            set { attribID = value; }
        }

        public string AttribName
        {
            get { return attribName; }
            set { attribName = value; }
        }

        public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }

        public bool AttribDisabled
        {
            get { return attribDisabled; }
            set { attribDisabled = value; }
        }

        public string AttribDescription
        {
            get { return attribDescription; }
            set { attribDescription = value; }
        }

        //***************** end of properties
        
        //  public List<Attrib> Attribs { get; set; }

        public string AddAttrib(string aN, string dT, bool aD, string aDesc)
        {             
            string results = string.Empty;
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    string sqlString = "insert into [ConfigurationManager].[dbo].[Attrib] VALUES (@attribName, @dataType, @attribDisabled, @attribDescription)";
                    conn.Open();
                    using (SqlCommand command = new SqlCommand(sqlString, conn))
                    {
                        
                        command.Parameters.AddWithValue("@attribName", aN);
                        command.Parameters.AddWithValue("@dataType", dT);
                        command.Parameters.AddWithValue("@attribDisabled",aD);
                        command.Parameters.AddWithValue("@attribDescription", aDesc);

                        int rows = command.ExecuteNonQuery();
                    
                        results = aN;

                       
                        
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                results = sqlEx.Message;
            }
            catch (Exception ex)
            {
                results = ex.Message;
            }

            return results;
        }

        public string UpdateAttrib(int aI, string aN, string dT, bool aD, string aDesc)
        {
            string results = "";
            try
            {

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    string sqlString = "UPDATE [ConfigurationManager].[dbo].[Attrib] SET attribName=@attribName, dataType=@dataType, attribDisabled=@attribDisabled, attribDescription=@attribDescription WHERE attribID=@attribID";
                    conn.Open();
                    using (SqlCommand command = new SqlCommand(sqlString, conn))
                    {
                        command.Parameters.AddWithValue("@attribID", aI);
                        command.Parameters.AddWithValue("@attribName", aN);
                        command.Parameters.AddWithValue("@dataType", dT);
                        command.Parameters.AddWithValue("@attribDisabled", aD);
                        command.Parameters.AddWithValue("@attribDescription", aDesc);

                        int rows = command.ExecuteNonQuery();
                        results = aN;
                        //rows number of record got updated
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                results = sqlEx.Message;
            }
            catch (Exception ex)
            {
                results = ex.Message;
            }

            return results;
        }

        public string DeleteAttrib(int aI)
        {
            string results = "";
            try
            {
                string sqlString = "DELETE FROM [dbo].[Attrib] WHERE attribID=@attribID";

                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    using (SqlCommand command = new SqlCommand(sqlString, conn))
                    {
                        command.Parameters.AddWithValue("@attribID", aI);

                        int rows = command.ExecuteNonQuery();

                        results = aI.ToString();
                        //rows number of record got deleted
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                results = sqlEx.Message;
            }
            catch (Exception ex)
            {
                results = ex.Message;
            }

            return results;

        }

              // need clarification (Don't we assign attribute to groups?) comment by amha  
            public string AssignAttrib()
            {
                string result = string.Empty;
                //code
                return result;            
            }

            public bool IsDisabled() // or would you rather have a return type as bool??
            {
                return true;
            }


            public List<Attrib> GetAllAttrib()
            {
                List<Attrib> AttribList = new List<Attrib>();

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    string queryString = "Select *  from  [ConfigurationManager].[dbo].[Attrib]";

                    try
                    {

                        connection.Open();

                        SqlCommand command = new SqlCommand(queryString, connection);

                        SqlDataReader reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            Attrib attrib = new Attrib();

                            attrib.attribID = reader.GetInt32(0);
                            attrib.attribName = reader.GetString(1);
                            attrib.dataType = reader.GetString(2);
                            attrib.attribDisabled = reader.GetBoolean(3);
                            attrib.attribDescription = reader.GetString(4);

                            AttribList.Add(attrib);
                        }

                    }

                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }

                    return AttribList;
                }
            }


            public  string SearchAttrib( string attribName)
            {

                String connectionString = ConfigurationManager.ConnectionStrings["Attrib"].ConnectionString;
                SqlConnection connection = new SqlConnection(connectionString);

                string result = "";
                Attrib attrib = new Attrib();

                try
                {
                    string select = "SELECT * FROM [ConfigurationManager].[dbo].[Attrib] WHERE AttribName ='" + attribName + "'";
                    connection.Open();
                    SqlCommand SearchCommand = new SqlCommand(select, connection);
                    SqlDataReader reader = SearchCommand.ExecuteReader();
                    while (reader.Read())
                    {

                        attrib.AttribID = Convert.ToInt32(reader[0]);
                        attrib.AttribName = reader.GetString(1);
                        attrib.DataType = reader[2].ToString();
                        attrib.AttribDisabled = Convert.ToBoolean(reader[3]);
                        attrib.AttribDescription = reader[4].ToString();
                                               
                    }
                    reader.Close();
                    connection.Close();
                    return attrib.attribName;

                }
                catch (System.Data.SqlClient.SqlException sqlException)
                {
                    result = sqlException.Message;
                }
                attrib.AttribName = result;
                return attrib.attribName;

                /*
                string result = string.Empty;
               

                try
                {
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        //Attrib attrib = new Attrib();
                        string select = "SELECT * FROM [ConfigurationManager].[dbo].[Attrib] WHERE AttribName ='" + AttribName + "'";
                        connection.Open();

                        SqlCommand SearchCommand = new SqlCommand(select, connection);
                                                
                        SqlDataReader reader = SearchCommand.ExecuteReader();
                        if (reader.HasRows)
                        {

                            while (reader.Read())
                            {

                                this.AttribID = Convert.ToInt32(reader[0]);
                                this.AttribName = reader.GetString(1);
                                this.DataType = reader[2].ToString();
                               // attrib.AttribDisabled = Convert.ToBoolean(reader[3]);
                                this.AttribDescription = reader[4].ToString();
                            }
                        }
                        reader.Close();

                        result = this.AttribName;                 
                                                             
                    }
                }
                
            

                catch (Exception e)
                {
                    result = (e.Message);
                }

                return result;
                */
           }
    }
}
